package com.destination.control;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.destination.model.ConnUtil;

public class ShowClassState extends Activity {

	private ConnUtil cu=new ConnUtil();
	private Dialog dlg=null;
	private String strResult=null;
	private ListView lv_classState=null;
	private classStateAsync async=null;
	private List<Map<String, String>> classState=null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tab_classstate);
		
		lv_classState=(ListView)findViewById(R.id.lv_classstate);
		
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		async=new classStateAsync();
		async.execute(100);
		super.onResume();
	}
	
	private class classStateAsync extends AsyncTask<Integer, Integer, String>{
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			dlg=new AlertDialog.Builder(ShowClassState.this)
				.setMessage("正在加载中……")
				.create();
			dlg.show();
			super.onPreExecute();
		}
		@Override
		protected String doInBackground(Integer... params) {
			// TODO Auto-generated method stub
			strResult=cu.getDisClassstateInfo();
			return strResult;
		}
		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub	
			classState=new ArrayList<Map<String,String>>();
			try {
				classState=parseClassstateMapJson(result);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			SimpleAdapter simple=new SimpleAdapter(ShowClassState.this, 
					classState, R.layout.classstate_item, 
					new String[]{"class_state_id","class_name","class_state_time","class_isObstain"},
					new int[]{R.id.txt_classstate_id,R.id.txt_classstate_name,R.id.txt_classstate_time,R.id.txt_classstate_obstain});
			lv_classState.setAdapter(simple);
			dlg.dismiss();		
			super.onPostExecute(result);
		}
	}
	
	
	public List<Map<String,String>> parseClassstateMapJson(String dataJson) throws Exception{
		
		List<Map<String,String>> classsStates=new ArrayList<Map<String,String>>();
		JSONArray jsonArr=new JSONArray(dataJson.toString().trim());
		for(int i=0;i<jsonArr.length();i++){		
			Map<String,String> classState=new HashMap<String, String>();
			JSONObject jsonObject=jsonArr.getJSONObject(i);
			classState.put("class_state_id",String.valueOf(jsonObject.getInt("class_state_id")));
			classState.put("class_name",jsonObject.getString("class_name"));
			if(jsonObject.getString("class_isObstain").equals("true")){
				classState.put("class_isObstain","占用");
			}else{
				classState.put("class_isObstain","空闲");
			}
			if(jsonObject.getString("class_begtime").equals("00:00:00")){
				classState.put("class_state_time", "");
			}
			else{
				classState.put("class_state_time", 
					jsonObject.getString("class_begtime").substring(0,jsonObject.getString("class_begtime").lastIndexOf(":"))+"-"+
					jsonObject.getString("class_endtime").substring(0,jsonObject.getString("class_endtime").lastIndexOf(":")));
			}
			classState.put("lessonSer_id", jsonObject.getString("lessonSer_id"));
			classsStates.add(classState);
		}
		return classsStates;
	}
}